//
//  CFGetCodeVC.m
//  CFfuturestalent
//
//  Created by 子yu 刘 on 2018/5/2.
//  Copyright © 2018年 子yu 刘. All rights reserved.
//

#import "CFGetCodeVC.h"
#import "CFSetDealPasswordVC.h"
#import "CFsetNickNameVC.h"


@interface CFGetCodeVC ()

@end

@implementation CFGetCodeVC{
    NSTimer * timer;
    int seconds;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.sendCodeBtn.layer.masksToBounds = YES;
    self.sendCodeBtn.layer.cornerRadius = 17.5;
    self.sendCodeBtn.layer.borderWidth = 1;
    self.sendCodeBtn.layer.borderColor = [UIColor colorWithHexString:@"cd1212"].CGColor;
    [self.tfCode setValue:[UIColor colorWithHexString:@"687278"]
                forKeyPath:@"_placeholderLabel.textColor"];
    [[self.tfCode rac_textSignal] subscribeNext:^(NSString * _Nullable x) {
        if (x.length>=6) {
            self.tfCode.text = [self.tfCode.text substringToIndex:6];
            [self.btnNext setEnabled:YES];
            self.btnNext.backgroundColor = DEF_COLOR_BTNNORCOLOR;
        }else{
            [self.btnNext setEnabled:NO];
            self.btnNext.backgroundColor = DEF_COLOR_BTNDELCOLOR;
        }
    }];
}
- (IBAction)sendCode:(id)sender {
    [self.sendCodeBtn setEnabled:NO];
    [self startTime];

}

- (IBAction)btnNextClicked:(id)sender {
    CFSetDealPasswordVC * setDealpassword = (CFSetDealPasswordVC *)[CFUtility getVC:@"CFSetDealPasswordVC" Storyboard:MINE];
    [self.navigationController pushViewController:setDealpassword animated:YES];
}

-(void)startTime {
    timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerStart:) userInfo:nil repeats:YES];
    seconds = 60;
}

-(void)timerStart:(NSTimer *)theTimer {
    if (seconds == 0) {
        [theTimer invalidate];
        seconds = 60;
        [self.sendCodeBtn setTitle:@"重新发送" forState: UIControlStateNormal];
//        self.sendCodeBtn.layer.borderColor = [UIColor colorWithHexString:@"cd1212"].CGColor;
//        [self.sendCodeBtn setTitleColor:[UIColor colorWithHexString:@"cd1212"] forState:UIControlStateNormal];
        [self.sendCodeBtn setEnabled:YES];
    }else{
        seconds--;
        NSString *title = [NSString stringWithFormat:@"重新获取 (%.2d)",seconds];
        [self.sendCodeBtn setEnabled:NO];
        [self.sendCodeBtn setTitle:title forState:UIControlStateNormal];
//         self.sendCodeBtn.layer.borderColor = [UIColor colorWithHexString:@"da7878"].CGColor;
//        [self.sendCodeBtn setTitleColor:[UIColor colorWithHexString:@"da7878"] forState:UIControlStateNormal];
    }
}


@end
